/*
* Copyright (C) 2024 ~ 2025 Uniontech Technology Co., Ltd.
*
* Author:      zhangbingyi <zhangbingyi@uniontech.com>
*
* Maintainer:  zhangbingyi <zhangbingyi@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "suggestdelegate.h"
#include "virusfiledetaildefinition.h"

#include <QPainter>
#include <QApplication>
#include <QToolTip>
#include <QMouseEvent>

//#include <DFontSizeManager>
#include <QComboBox>

#define ICON_SIZE 12
#define ICON_SPACE 10
#define TEXT_SPACE 10

SuggestDelegate::SuggestDelegate(QObject *parent)
    : VirusFileDetailDelegate(parent)
{
}

void SuggestDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    if (index.column() == COLUMN_SUGGESTION) {
        QModelIndex curIndex = index.model()->index(index.row(), COLUMN_SUGGESTION);
        bool isOperating = curIndex.data(ROLE_OPERATING_FLAG).toBool();
        if (isOperating) {
            //绘制文字
            painter->save();
            //int currentFontSize = DFontSizeManager::instance()->fontPixelSize(qApp->font());
            int currentFontSize = qApp->font().pixelSize();
            QFont font;
            font.setPixelSize(currentFontSize);
            QRect textRect = itemOption.rect;
            QString strOperating = QString(tr("To be repaired"));
            int heightSpacing = (textRect.height() - currentFontSize) / 2;
            QPoint textPoint(textRect.x() + ICON_SIZE + ICON_SPACE + TEXT_SPACE, textRect.y() + heightSpacing + ICON_SIZE);
            painter->setFont(font);
            QFontMetrics fontWidth(font);
            strOperating = fontWidth.elidedText(strOperating, Qt::ElideMiddle, itemOption.rect.width() - ICON_SIZE - ICON_SPACE - TEXT_SPACE);
            painter->drawText(textPoint, strOperating);
            painter->restore();
        } else {
            //绘制图标
            painter->save();
            int tmpStatus = curIndex.data(ROLE_SUGGESTION_STATUS).toInt();
            QRect rect = itemOption.rect;
            int heightSpacing = (rect.height() - ICON_SIZE) / 2;
            rect.setRect(rect.x() + ICON_SPACE, rect.y() + heightSpacing, ICON_SIZE, ICON_SIZE);
            if (tmpStatus == 1 || tmpStatus == 2 || tmpStatus == 3) {
                painter->drawPixmap(rect, QIcon::fromTheme("dcc_antiav_normal").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
            } else {
                painter->drawPixmap(rect, QIcon::fromTheme("dcc_antiav_warning").pixmap(QSize(ICON_SIZE, ICON_SIZE)));
            }
            painter->restore();

            //绘制文字
            painter->save();
            QString strSuggest;
            //操作类型   0未操作  1 修复  2 隔离  3信任
            switch (tmpStatus) {
            case 1:
                strSuggest = QString(tr("Repaired"));
                break;
            case 2:
                strSuggest = QString(tr("Quarantined"));
                break;
            case 3:
                strSuggest = QString(tr("Trusted"));
                break;
            default:
                strSuggest = QString(tr("To be repaired"));
                break;
            }
//            int currentFontSize = DFontSizeManager::instance()->fontPixelSize(qApp->font());
            int currentFontSize = qApp->font().pixelSize();
            QFont font;
            font.setPixelSize(currentFontSize);
            QRect textRect = itemOption.rect;
            heightSpacing = (textRect.height() - currentFontSize) / 2;
            QPoint textPoint(textRect.x() + ICON_SIZE + ICON_SPACE + TEXT_SPACE, textRect.y() + heightSpacing + ICON_SIZE);
            painter->setFont(font);
            QFontMetrics fontWidth(font);
            strSuggest = fontWidth.elidedText(strSuggest, Qt::ElideMiddle, itemOption.rect.width() - ICON_SIZE - ICON_SPACE - TEXT_SPACE);
            painter->drawText(textPoint, strSuggest);
            painter->restore();
        }
    }
    return VirusFileDetailDelegate::paint(painter, option, index);
}

bool SuggestDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    return VirusFileDetailDelegate::editorEvent(event, model, option, index);
}

QWidget *SuggestDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    Q_UNUSED(option);
    if (index.column() == COLUMN_SUGGESTION) {
//        DSpinner *spinner = new DSpinner(parent);
//        spinner->setFixedSize(16, 16);
//        spinner->show();
//        spinner->start();
//        return spinner;
    }
    return nullptr;
}

void SuggestDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem tmp(option);
    if (index.column() == COLUMN_SUGGESTION) {
        tmp.rect.adjust(6, 21, 0, 0);
        VirusFileDetailDelegate::updateEditorGeometry(editor, tmp, index);
        return;
    }
    VirusFileDetailDelegate::updateEditorGeometry(editor, option, index);
}

bool SuggestDelegate::helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index)
{
    if (event->type() == QEvent::ToolTip) {
        if (index.column() == COLUMN_SUGGESTION) {
            QStyleOptionViewItem itemOption(option);
            QRect rect = itemOption.rect;

            QRect suggestRect(rect.x() + 30, rect.y() + 20, itemOption.rect.width() - 40, option.fontMetrics.height());
            int tmpStatus = index.data(ROLE_SUGGESTION_STATUS).toInt();
            QString tooltip;
            if (suggestRect.contains(event->pos())) {
                switch (tmpStatus) {
                case 1:
                    tooltip = QString(tr("Repaired"));
                    break;
                case 2:
                    tooltip = QString(tr("Quarantined"));
                    break;
                case 3:
                    tooltip = QString(tr("Trusted"));
                    break;
                default:
                    tooltip = QString(tr("To be repaired"));
                    break;
                }
            } else {
                QWidgetList qwl = QApplication::topLevelWidgets();
                for (QWidget *qw : qwl) {
                    if (QString("QTipLabel") == qw->metaObject()->className()) {
                        qw->close();
                    }
                }
            }
            QToolTip::showText(event->globalPos(), tooltip);
            return true;
        }
    }
    return VirusFileDetailDelegate::helpEvent(event, view, option, index);
}
